//
//  AppDelegate.m
//  海角音乐
//
//  Created by dllo on 15/8/4.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "RightViewController.h"
#import <AFNetworking.h>


// 推送
#import "XGPush.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc
{
    [_leftVc release];
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
              // access Id         app key
    [XGPush startApp:2200144915 appKey:@"I4178SLPYG3B"];
    
    // 推送push 通知
    // 判断当前 系统的版本
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
        
        // 得到我的app 程序
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]];
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        
        
    } else {
        
        // iOS 7.0
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
    }
    
    
    
    [NSThread sleepForTimeInterval:2];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    MainViewController *rootVC = [[MainViewController alloc]init];
    
    UINavigationController *naVc = [[UINavigationController alloc]initWithRootViewController:rootVC];
    [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
    
    
    // 然后就可以使用下面的代码来设置状态栏风格 先修改info.plist 增加一个新的key 取消原有的外观
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    
    // 单独创建的VC  作为抽屉的View
    RightViewController *cVC = [[RightViewController alloc]init];
    self.leftVc = [[LeftSlideViewController alloc] initWithLeftView:cVC andMainView:naVc];
    // 把标签控制器设置为根视图控制器
    self.window.rootViewController = self.leftVc;
    [self.window makeKeyAndVisible];
    
    DataBase *data = [DataBase shareDataBaseHandle];
    [data openDB];
    [self reach];
    
    
    //后台播放音频设置
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    

    
    return YES;
}


// 三个监听方法
// 接收通知
// 出现提示框的方法进入程序
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    
    
    
}
// 注册成功 返回token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    
    // 返回一个 NSData 的数据
    [XGPush registerDevice:deviceToken];
    
}

// 注册失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    
    NSLog(@"error =    %@", error);
    
    
    
}

- (void)reach
{

    //判断网络
    AFNetworkReachabilityManager *mpg = [AFNetworkReachabilityManager sharedManager];
    [mpg setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"当前是WIFI环境");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"当前是手机网络");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"当前没有网络");
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"没有网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重新链接", nil];
                [alter show];
                [alter release];
                break;
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未知网络");
                break;
            default:
                break;
        }
        
    }];
    [mpg startMonitoring];
}


// 点了哪个按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        AFNetworkReachabilityManager *reachManager = [AFNetworkReachabilityManager sharedManager];
        if (reachManager.reachable) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Internet" object:nil];
            
        }else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"网络警示" message:@"当前没有网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重新连接", nil];
            [alertView show];
            [alertView release];
            
            
        }
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    
    [application beginReceivingRemoteControlEvents];
    
    
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
